package com.tuananh.radnom.persist.imp.xml;

import java.util.ArrayList;
import java.util.StringTokenizer;

import com.ajexperience.utils.DeepCopyException;
import com.ajexperience.utils.DeepCopyUtil;
import com.tuananh.radnom.model.netpoller.PollScheduleInfo;
import com.tuananh.radnom.model.netpoller.PollMapEntry;
import com.tuananh.radnom.model.netpoller.PollPackage;
import com.tuananh.radnom.model.netpoller.PollingConfiguration;
import com.tuananh.radnom.model.netpoller.PollingService;
import com.tuananh.radnom.model.netservice.NetService;
import com.tuananh.radnom.persist.IPollingServiceDAO;

public class PollServiceXmlDAO extends XmlDAO implements IPollingServiceDAO {
	PollingConfiguration pollConfig;

	public PollServiceXmlDAO() {
		super(PollingConfiguration.path);
		configuration = XmlConfigHolder.pollingConfig;
		try {
			savedConfiguration = (new DeepCopyUtil()).deepCopy(configuration);
		} catch (DeepCopyException e) {
			// TODO Auto-generated catch block
			savedConfiguration = new PollingConfiguration();
			e.printStackTrace();
		}
		pollConfig = (PollingConfiguration) configuration;
	}

	public PollPackage getByName(String packageName) {
		// TODO Auto-generated method stub
		for(PollPackage pollPack:pollConfig.getPollPackage()){
			if (pollPack.getName().equals(packageName))
				return pollPack;
		}
		return null;
	}
	public ArrayList<PollPackage> getAllPackage() {
		// TODO Auto-generated method stub
		return pollConfig.getPollPackage();
	}
	public boolean addPollPackage(PollPackage pollPack) {
		// TODO Auto-generated method stub
		if (getByName(pollPack.getName())==null){
			pollConfig.getPollPackage().add(pollPack);
			return true;
		}
		return false;
	}
	public boolean removePollPackage(String packageName) {
		// TODO Auto-generated method stub
		PollPackage pollPack = getByName(packageName);
		if (pollPack!=null){
			pollConfig.getPollPackage().remove(pollPack);
			return true;
		}
		return false;
	}
	public boolean updatePollPackage(PollPackage pollPack) {
		// TODO Auto-generated method stub
		for (int i=0;i<pollConfig.getPollPackage().size();i++)
		{
			if (pollConfig.getPollPackage().get(i).getName().equals(pollPack.getName())){
				pollConfig.getPollPackage().set(i, pollPack);
				return true;
			}
		}
		return false;
	}

	public boolean addService(String packageName, NetService netService) {
		// TODO Auto-generated method stub
		if (packageName==null) return false;
		PollPackage pollPack = getByName(packageName);
		if (pollPack!=null){
			for (PollPackage temp:pollConfig.getPollPackage()){
				if (temp.getName().equals(pollPack.getName()))
				{
					return false;
				}
			}
			pollConfig.getPollPackage().add(pollPack);
			return true;
		}
		return false;
	}
	public boolean removeService(String packageName, String serviceName) {
		// TODO Auto-generated method stub
		if (packageName==null || serviceName==null) return false;
		PollPackage pollPack = getByName(packageName);
		if (pollPack!=null){
			for (int i=0;i<pollPack.getServices().size();i++){
				if (pollPack.getServices().get(i).getName().equals(serviceName)){
					pollPack.getServices().remove(i);
					return true;
				}
			}
		}
		return false;
	}
	public boolean updateService(String packageName, NetService netService) {
		// TODO Auto-generated method stub
		if (packageName==null || netService==null) return false;
		PollPackage pollPack = getByName(packageName);
		if (pollPack!=null){
			for (int i=0;i<pollPack.getServices().size();i++){
				if (pollPack.getServices().get(i).getName().equals(netService.getName())){
					pollPack.getServices().add(i, netService);
					return true;
				}
			}
		}
		return false;
	}
	public NetService getService(String packageName, String serviceName) {
		// TODO Auto-generated method stub
		if (packageName==null || serviceName==null) return null;
		PollPackage pollPack = getByName(packageName);
		if (pollPack!=null){
			for (int i=0;i<pollPack.getServices().size();i++){
				if (pollPack.getServices().get(i).getName().equals(serviceName)){
					return pollPack.getServices().get(i);
				}
			}
		}
		return null;
	}
	public boolean addNetInt(String packageName, String netIntName) {
		// TODO Auto-generated method stub
		if (packageName==null || netIntName==null) return false;
		PollPackage pollPack = getByName(packageName);
		if (pollPack!=null){
			String listInt = pollPack.getNetInt();
			if (!listInt.contains(netIntName)){
				pollPack.setNetInt(listInt+","+netIntName);
				return true;
			}
		}
		return false;
	}
	public boolean removeNetInt(String packageName, String netIntName) {
		if (packageName==null || netIntName==null) return false;
		PollPackage pollPack = getByName(packageName);
		if (pollPack!=null){
			String add="";
			StringTokenizer token = new StringTokenizer(pollPack.getNetInt(), ",");
			while (token.hasMoreElements()){
				add+=token.nextToken()+",";
			}
			pollPack.setNetInt(add);
			return true;
		}
		return false;
	}
	
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "PollServiceEvent";
	}
}
